"""
@File    :   route_depend.py
@Time    :   2024/03/28 23:35:28
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   路由依赖(用于维持本业务的路由部分整洁)
"""

from typing import Annotated

from fastapi import Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession

from src.business.admin.system.oauth2.client.schemas.out import (
    Oauth2ClientCompleteOutSchema,
)
from src.business.admin.system.oauth2.client.schemas.validate import (
    Oauth2ClientCreateValidateSchema,
    Oauth2ClientDeleteValidateSchema,
    Oauth2ClientListSearchValidateSchema,
    Oauth2ClientUpdateStatusValidateSchema,
    Oauth2ClientUpdateValidateSchema,
)
from src.business.admin.system.oauth2.client.service import Oauth2ClientService
from src.business.admin.system.oauth2.token.dependencies.no_introduction import (
    GetTenantIdFromAccessTokenDependencies,
    GetUserNameFromAccessTokenDependencies,
)
from src.dependencies.no_introduction import depend_get_master_db_session
from src.schemas.validate import (
    OrderBySearchValidateSchema,
    PaginationSearchValidateSchema,
)


async def route_depend_get_oauth2_client(
    oauth2_client_id: Annotated[
        int, Annotated[int, Query(..., ge=1, description="Oauth2客户端ID")]
    ],
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> Oauth2ClientCompleteOutSchema:
    """查询单个OAuth2客户端的路由依赖

    Args:
        oauth2_client_id (Annotated[int, Depends): Oauth2客户端ID
        tenant_id: 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        Oauth2ClientCompleteOutSchema: Oauth2客户端信息
    """
    oauth2_client: Oauth2ClientCompleteOutSchema = (
        await Oauth2ClientService.get_oauth2_client_use_id(
            db_session,
            tenant_id,
            oauth2_client_id,
            raise_not_exists_exception=False,
            raise_disabled_exception=False,
            raise_expired_exception=False,
        )
    )
    return oauth2_client


async def route_depend_list_oauth2_client(
    search_schema: Oauth2ClientListSearchValidateSchema,
    pagination_schema: PaginationSearchValidateSchema,
    order_schema: OrderBySearchValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> tuple[list[Oauth2ClientCompleteOutSchema], int]:
    """查询OAuth2客户端列表的路由依赖

    Args:
        search_schema (Annotated[TenantListSearchValidateSchema, Depends): 查询参数
        pagination_schema (Annotated[PaginationSearchValidateSchema, Depends): 分页参数
        order_schema (Annotated[OrderBySearchValidateSchema, Depends): 排序参数
        tenant_id: 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        tuple[list[Oauth2ClientCompleteOutSchema], int]: 查询结果
    """
    # 注解
    search_pagination_result: list[Oauth2ClientCompleteOutSchema]
    search_total: int
    # 列表结果查询
    (
        search_pagination_result,
        search_total,
    ) = await Oauth2ClientService.list_search_oauth2_client(
        db_session, tenant_id, search_schema, pagination_schema, order_schema
    )
    return search_pagination_result, search_total


async def route_depend_create_oauth2_client(
    create_schema: Oauth2ClientCreateValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> int:
    """创建OAuth2客户端的依赖

    Args:
        create_schema (GetTenantIdFromAccessTokenDependencies): 创建所需的参数
        tenant_id: 当前token指向的租户ID
        username: 当前token指向的OAuth2客户端名
        db_session: 数据库连接池

    Returns:
        int: 创建的sql的主键ID
    """
    # 创建OAuth2客户端
    created_id = await Oauth2ClientService.create_oauth2_client(
        db_session, tenant_id, create_schema, creator_username=username
    )
    return created_id


async def route_depend_update_oauth2_client(
    update_schema: Oauth2ClientUpdateValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """修改OAuth2客户端的路由依赖

    Args:
        update_schema (Oauth2ClientUpdateValidateSchema): 修改所需的参数
        tenant_id: 当前token指向的租户ID
        username: 当前token指向的OAuth2客户端名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await Oauth2ClientService.update_oauth2_client(
        db_session, tenant_id, update_schema, updater_username=username
    )


async def route_depend_update_oauth2_client_status(
    update_schema: Oauth2ClientUpdateStatusValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """单独修改OAuth2客户端状态的路由依赖

    Args:
        update_schema (Oauth2ClientUpdateStatusValidateSchema): 修改所需的参数
        tenant_id: 当前token指向的租户ID
        username: 当前token指向的OAuth2客户端名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await Oauth2ClientService.update_oauth2_client_status(
        db_session, tenant_id, update_schema, updater_username=username
    )


async def route_depend_delete_oauth2_client(
    delete_schema: Oauth2ClientDeleteValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """删除OAuth2客户端的路由依赖

    Args:
        delete_schema (Oauth2ClientDeleteValidateSchema): 删除所需的参数
        tenant_id: 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 删除操作
    await Oauth2ClientService.delete_oauth2_client(db_session, tenant_id, delete_schema)
